package com.qding.park.adapter.ark.adapter.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qding.park.adapter.ark.adapter.config.ARKConfig;
import com.qding.park.adapter.ark.adapter.constant.MethodConstant;
import com.qding.park.adapter.ark.adapter.entity.ResponseMsg;
import com.qding.park.adapter.ark.adapter.enums.InvitationEventEnum;
import com.qding.park.adapter.ark.adapter.enums.LockEventEnum;
import com.qding.park.adapter.ark.adapter.enums.RespCodeEnums;
import com.qding.park.adapter.ark.adapter.util.Helper;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.system.NetWorkException;
import com.qding.park.common.core.network.HttpUtil;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.target.common.adapter.LockCarAdapter;
import com.qding.park.target.common.entity.AdLockCarOrder;

/**
 * @ClassName LockCarAdapterImpl
 * @Description TODO(锁车功能实现)
 * @author LeiYang
 * @Date 2017年5月9日 上午10:07:38
 * @version 1.0.0
 */
@Service("lockCarAdapter_13")
public class LockCarAdapterImpl implements LockCarAdapter {

    private static final Logger _log = LoggerFactory.getLogger(LockCarAdapterImpl.class);

    @Override
    public void createLockCarOrder(AdLockCarOrder apLockCarOrder) throws AdapterException {
        String url = ARKConfig.getAdapterUrl();
        String urlMethod=Helper.createUrlAddress(url, MethodConstant.LOCKCAR);
        String orderId = apLockCarOrder.getOrderId();
        String parkId = apLockCarOrder.getParkCode();
        String carNo = apLockCarOrder.getCarNum();
        String personId = apLockCarOrder.getUserId();
        String lockEvent = LockEventEnum.CARLOCK.getValue();
        String startTime = DateUtils.formatDate(apLockCarOrder.getStartTime(), DateUtils.DATE_FORMAT_DATETIME);
        String endTime = DateUtils.formatDate(apLockCarOrder.getEndTime(), DateUtils.DATE_FORMAT_DATETIME);

        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId);
        params.put("parkId", parkId);
        params.put("carNo", carNo);
        params.put("personId", personId);
        params.put("lockEvent", lockEvent);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        try {
            String result = HttpUtil.doPost(urlMethod, params);
            ResponseMsg response = Helper.analysisResponse(result);

            if (!RespCodeEnums.SUCCESS.getValue().equals(response.getCode())) {
                // 失败
                ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                        AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), response.getMsg(),
                        apLockCarOrder.toString());

                throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
                        exceptionMessage);
            }
        } catch (NetWorkException | IOException e) {
            _log.error(e.getMessage(), e);
            ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                    AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), "方舟接口网络暖异常", apLockCarOrder.toString());
            throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), e, exceptionMessage);
        }
    }

    @Override
    public void unLockCar(AdLockCarOrder apLockCarOrder) throws AdapterException {
        String url = ARKConfig.getAdapterUrl();
        String urlMethod=Helper.createUrlAddress(url, MethodConstant.LOCKCAR);
        String orderId = apLockCarOrder.getOrderId();
        // String parkId = apLockCarOrder.getParkCode();
        // String carNo = apLockCarOrder.getCarNum();
        String personId = apLockCarOrder.getUserId();
        String lockEvent = LockEventEnum.CARLOCK.getValue();
        // String startTime = DateUtils.formatDate(apLockCarOrder.getStartTime(), DateUtils.DATE_FORMAT_DATETIME);
        // String endTime = DateUtils.formatDate(apLockCarOrder.getEndTime(), DateUtils.DATE_FORMAT_DATETIME);

        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId);
        // params.put("parkId", parkId);
        // params.put("carNo", carNo);
        params.put("personId", personId);
        params.put("lockEvent", lockEvent);
        // params.put("startTime", startTime);
        // params.put("endTime", endTime);
        try {
            String result = HttpUtil.doPost(urlMethod, params);
            ResponseMsg response = Helper.analysisResponse(result);
            if (!RespCodeEnums.SUCCESS.getValue().equals(response.getCode())) {
                // 失败
                ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                        AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), response.getMsg(),
                        apLockCarOrder.toString());

                throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
                        exceptionMessage);
            }
        } catch (NetWorkException | IOException e) {
            _log.error(e.getMessage(), e);
            ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                    AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), "方舟接口网络暖异常", apLockCarOrder.toString());
            throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), e, exceptionMessage);
        }
    }

}
